from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_cors import CORS
from flask_restx import Api
from app.config import Config

# 初始化扩展
db = SQLAlchemy()
api = Api(title='文件存储系统API', version='1.0', description='文件存储系统的API接口')


def create_app(config_class=Config):
    app = Flask(__name__)
    app.config.from_object(config_class)

    # 初始化扩展
    db.init_app(app)
    # 配置CORS以允许所有来源访问API
    CORS(app, resources={r"/api/*": {"origins": "*"}})
    api.init_app(app)

    # 注册蓝图
    from app.routes.departments import ns as departments_ns
    from app.routes.files import ns as files_ns
    api.add_namespace(departments_ns, path='/api/departments')
    api.add_namespace(files_ns, path='/api/files')

    return app